import React from 'react';
import classNames from 'classnames';
import Menu from './Menu';

import './Menu.less';

class MenuComp extends React.Component {
    displayName = 'MenuComp';

    static defaultProps = {
        mode: 'vertical',
        level: 0,
        offset: 24,
        theme: 'light',
        accordion: false
    }

    state = {
        _id: 1
    }

    lastOpenSubmenu = null;
    lastSelectMenuItem = null;

    constructor (props) {
        super(props);

        this.scanData(props.data);
    }

    scanData (data) {
        data.forEach(item => {
            if (item.selected && item.type === 'MenuItem') {
                this.lastSelectMenuItem = item;
            }
            if (item.children && item.children.length) {
                this.scanData(item.children);
            }
        });
    }

    update (callback) {
        this.setState({
            _id: this.state._id + 1
        }, callback);
    }

    onSelectSubMenu = (subMenu, itemData) => {
        const {accordion} = this.props;
        if (itemData.open) {
            itemData.open = false;
        } else {
            itemData.open = true;
            if (accordion && this.lastOpenSubmenu && this.lastOpenSubmenu != itemData) {
                if (this.currentIsLastChildren(itemData)) {
                    // todo
                } else {
                    this.lastOpenSubmenu.open = false;
                }
            }
        }
        this.lastOpenSubmenu = itemData;

        this.update();
        this.props.onSelectSubMenu && this.props.onSelectSubMenu(itemData, subMenu);
    }

    onSelectMenu = (menuItem, itemData) => {
        itemData.selected = true;
        
        if (this.lastSelectMenuItem && this.lastSelectMenuItem !== itemData) {
            this.lastSelectMenuItem.selected = false;
        }
        this.lastSelectMenuItem = itemData;
        this.unSelectAllSubmenu(this.props.data);
        this.selectParents(itemData);
        this.update();

        this.props.onSelectMenu && this.props.onSelectMenu(itemData, menuItem);
    }

    unSelectAllSubmenu (items) {
        items.forEach(item => {
            if (item.type === 'SubMenu') {
                item.selected = false;
                if (item.children && item.children.length) {
                    this.unSelectAllSubmenu(item.children);
                }
            }
            if (item.type === 'MenuGroup') {
                if (item.children && item.children.length) {
                    this.unSelectAllSubmenu(item.children);
                }
            }
        });
    }

    selectParents (item) {
        if (item) {
            if (item.type === 'SubMenu') {
                item.selected = true;
            }
        }
        if (item.parent) {
            this.selectParents(item.parent);
        }
    }

    /**
     * 当前选中的submenu为上次选中的孩子节点
     * @param {*} submenu 
     */
    currentIsLastChildren (submenu) {
        if (!this.lastOpenSubmenu) {
            return false;
        }
        let parent = submenu.parent;
        while (parent) {
            if (this.lastOpenSubmenu == parent) {
                return true;
            }
            parent = parent.parent;
        }
        return false;
    }

    render () {
        return <Menu {...this.props} onSelectMenu={this.onSelectMenu} onSelectSubMenu={this.onSelectSubMenu}/>;
    }
}

export default MenuComp;
